﻿package com.addcn.texas.view.components
{	
	import com.addcn.common.event.ParamEvent;
	import com.addcn.texas.event.FriendItemEvent;
	import fl.containers.ScrollPane;
	import fl.events.ListEvent;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import fl.controls.*
	import flash.text.*;		
	import fl.data.DataProvider;
	
	public class FriendBox extends MovieClip	
	{		
		public static const REFRESH_LIST : String = "refreshList";
		public static const JOIN_ROOM : String = "joinRoom";
		public static const CHANGE_TYPE_EVENT : String = "changeTypeEvent";
		public static const CHANGE_EVENT : String = "changeEvent";
		public static const DOUBLE_CLICK : String = "doubleClick";		
		private var _type : int;
		private var _itme_list : FriendItiemList;
		
		function FriendBox () {
			_inizialize();		
		}		
		
		public function _inizialize() {					
			this["btn1"].addEventListener(MouseEvent.CLICK, _btnClickHandler);
			this["btn1"].addEventListener(MouseEvent.MOUSE_MOVE, _btnOverHandler);
			this["btn1"].addEventListener(MouseEvent.MOUSE_OUT, _btnOutHandler);
			this["btn1"].gotoAndStop("up");
			this["btn1"].buttonMode = true;				
			this["btn2"].addEventListener(MouseEvent.CLICK, _btnClickHandler);
			this["btn2"].addEventListener(MouseEvent.MOUSE_MOVE, _btnOverHandler);
			this["btn2"].addEventListener(MouseEvent.MOUSE_OUT, _btnOutHandler);
			this["btn2"].buttonMode = true;		
			this["btn2"].gotoAndStop("up");
			
			_itme_list = new FriendItiemList;	
			_itme_list.addEventListener(FriendItemEvent.ITEM_CLICK, _itemEventHandler);
			_itme_list.addEventListener(FriendItemEvent.ITEM_OVER, _itemEventHandler);
			_itme_list.addEventListener(FriendItemEvent.ITEM_OUT, _itemEventHandler);
			_itme_list.addEventListener(FriendItemEvent.ITEM_DOUBLE_CLICK, _itemEventHandler);			
						
			upDateList([ {id:1,name:"aaaa",gold:45631313,state:1},{id:1,name:"aaaa",gold:45631313,state:1},{id:1,name:"aaaa",gold:45631313,state:1},{id:1,name:"aaaa",gold:45631313,state:0},{id:1,name:"aaaa",gold:45631313,state:0} ]);		
		}	
						
		public function listType(r_type : int) : void
		{
			
		}
		
		/**
		 * 設置好友列表數據
		 * @param	r_data Object [{id,name,gold,photourl},...]
		 */
		public function upDateList(r_data : Object) : void
		{			
			if (!r_data)  return;					
			var _fsp : ScrollPane = this["FriendScrollPane"];
			var _val : Object;
			var _id : int;
			var _name : String;
			var _gold ; String;
			var _url : String;
			var _state : int;
			for each(_val in r_data) {	
				_id = _val["id"];
				_name = _val["name"];
				_gold = _val["gold"];
				_state = _val["state"];
				var _item :FriendItem = new FriendItem(_id,_name,_gold,_state);
				_itme_list.addItemd(_item);								
			}		
			_fsp.source = _itme_list;  
			_fsp.horizontalScrollPolicy = ScrollPolicy.OFF;						
		}
						
		/**
		 * ==================================================
		 * 响应顶部类别按钮事件
		 * ==================================================
		 */
		private function _btnClickHandler(evt : Event) : void
		{
			var _btn : Object = this.getChildByName("btn" + this._type);
			if(_btn){
				_btn.gotoAndStop("up");		
			}
			var _str : String = evt.target.name;
			this._type = parseInt(_str.substr(3));
			var _event : ParamEvent = new ParamEvent(CHANGE_TYPE_EVENT, { type:_type } );
			dispatchEvent(_event);			
			evt.target.gotoAndStop("down");					
		}
		
		private function _btnOverHandler(evt: Event) : void
		{
			var _m_type : int = parseInt(evt.target.name.substr(3));
			if(_type != _m_type){
				evt.target.gotoAndStop("over");
			}
		}
		
		private function _btnOutHandler(evt: Event) : void
		{
			var _m_type : int = parseInt(evt.target.name.substr(3));
			if(_type != _m_type){
				evt.target.gotoAndStop("up");
			}
		}
		
		/**
		 * 响应FriendItem发送的事件
		 */
		private function _itemEventHandler(evt : FriendItemEvent) : void
		{
			trace("event : " + evt.type + "   "+evt.params.id)
			dispatchEvent(evt.clone());
		}
		
	}
}
